﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// WarehouseUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class WarehouseUserControl : UserControl, IPlugin
    {
        private WarehouseService warehouseService = ServiceContainer.GetService<WarehouseService>();

        public WarehouseUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "发货仓库"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.dgvWarehouse.ItemsSource = this.warehouseService.GetAll().Datas;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var warehouse = this.dgvWarehouse.SelectedItem as Mengsk.ProxyDelivery.Domain.Warehouse;
                if (warehouse == null)
                {
                    MessageBox.Show("请选择仓库");
                    return;
                }
                var window = new WarehouseEditWindow { Warehouse = warehouse };
                bool? ret = window.ShowDialog();
                if (ret != null && ret.Value)
                {
                    var item = this.dgvWarehouse.ItemsSource;
                    this.dgvWarehouse.ItemsSource = null;
                    this.dgvWarehouse.ItemsSource = item;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var window = new WarehouseEditWindow { };
                bool? ret = window.ShowDialog();
                if (ret != null && ret.Value)
                {
                    var item = this.dgvWarehouse.ItemsSource.OfType<Mengsk.ProxyDelivery.Domain.Warehouse>().ToList();
                    item.Add(window.Warehouse);
                    this.dgvWarehouse.ItemsSource = null;
                    this.dgvWarehouse.ItemsSource = item;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
